<?php
// +----------------------------------------------------------------------
// | Description: 1
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-24 16:50:46
// +----------------------------------------------------------------------

namespace app\client\model;

use app\common\model\CommonClient;

class Member extends CommonClient
{
    protected $fuzzyConditions = ['UserName', 'RealName', 'CellPhone', 'Email', 'QQ'];
    protected $clearConditions = ['Enabled', 'Id', 'ReferUserId'];
    protected $returnFields = '
        shop_members.Id,
        shop_members.GradeId,
        shop_members.UserName,
        shop_members.ReferUserName,
        shop_members.ReferCellPhone,
        shop_members.CreateDate,
        IFNULL(shop_members.BirchDate, false) as BirchDate,
        shop_members.RealName,
        shop_members.IdCard,
        shop_members.Email,
        shop_members.HeadImg,
        shop_members.Expend,
        shop_members.Points,
        shop_members.Balance,
        shop_members.CellPhone,
        shop_members.QQ,
        shop_members.WeChat,
        shop_members.Enabled,
        shop_members.OrderStatusZero,
        shop_members.OrderStatusOne,
        shop_members.OrderStatusTwo,
        shop_members.OrderStatusThree,
        shop_members.OrderStatusFour,
        shop_members.OrderStatusFive
    ';
    protected $rangeConditions = null;
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = null;
    protected $name = 'shop_members';

    /**
     * 解析搜索条件
     */
    public function getDataList()
    {
        $this->page = array_key_exists('page', $this->param) ? $this->param['page'] : 1;
        $this->limit = array_key_exists('limit', $this->param) ? $this->param['limit'] : 20;
        $this->orderByString = array_key_exists('orderByString', $this->param) ? $this->param['orderByString'] : null;
        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where($this -> name.'.IsDelete', 0);

        if ($this->limit == -1) {
            $resData = $this->field($this->returnFields)->select();
        } else {
            $resData = $this->field($this->returnFields)->page($this->page, $this->limit)->select();
        }
// p($this -> getlastsql());
        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where('IsDelete', 0);

        $dataCount = $this->count();
        $data['list'] = $resData;
        $data['dataCount'] = $dataCount;
        return $this->filter($data);
    }

    public function address($param)
    {
        $data = $this 
            -> where('shop_members_address.UserId', $param['UserId']) 
            -> join('shop_members_address', 'shop_members.AddressId = shop_members_address.Id')
            -> field('
                shop_members_address.Id,
                shop_members_address.ReceiverPhone,
                shop_members_address.ReceiverName,
                shop_members_address.AddrProvince,
                shop_members_address.AddrCity,
                shop_members_address.AddrArea,
                shop_members_address.AddrDetail
            ')
            -> find();
        return $data;
    }

    /**
     * [createData 新建]
     * @linchuangbin
     * @DateTime  2017-02-10T21:19:06+0800
     * @param     array                    $param [description]
     * @return    [array]                         [description]
     */
    public function createData($param, $scene = 'signup')
    {
        // 验证
        $validate = validate($this->name);
        if (!$validate->scene($scene)->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->data($param)->allowField(true)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = '注册失败!';
            return false;
        }
    }

    /**
     * 通过id修改用户
     * @param  array   $param  [description]
     */
    public function updateDataById($param, $id, $fields = true, $pk = "Id")
    {
        // echo $id;
        // p($param);
        try {
            $fields = 'UserName,BirchDate,RealName,IdCard,Email,HeadImg,AddressId,QQ,WeChat';
            $this->allowField($fields)->save($param, [$pk => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '编辑失败';
            return false;
        }
    }
}
